﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace boda.net.wpf.hardware.scanner;
public abstract class ScannerBase : IScanner
{
    public ScannerConfig? _scannerConfig { get; private set; }
    public bool Initialized { get; private set; }

    public event Action<string> BarcodeEvent;

    public void Close()
    {
        if(!Initialized)return; 
        Initialized = false;
        DoClose();
    }

    public bool Init(ScannerConfig scannerConfig)
    {
        _scannerConfig = scannerConfig;
        if(Initialized) return true;
        Initialized = DoInit();
        return Initialized;
    }

    protected void InvokeOnGrabbed(string barcode)
    {
        if(BarcodeEvent == null) return;
        foreach (Action<string> item in BarcodeEvent.GetInvocationList())
        {
            item.Invoke(barcode);
        }
    }

    public abstract void DoClose();
    public abstract bool DoInit();
    public abstract bool Trigger();
}
